part of 'page.dart';

class _AccountPageView extends StatelessWidget {
  const _AccountPageView({
    required _AccountPageWloc wloc,
  }) : _wloc = wloc;

  final _AccountPageWloc _wloc;

  static const _kSideNavigationWidth = 300.0;

  Widget _buildContent({
    required _AccountPageWlocState state,
    required BoxConstraints constraints,
    required BuildContext context,
  }) {
    final maxWidth = constraints.maxWidth;

    if (maxWidth < 800) {
      return _buildContent_0_800(
        state: state,
        constraints: constraints,
        context: context,
      );
    }

    return _buildContent_800_infinity(
      state: state,
      constraints: constraints,
      context: context,
    );
  }

  Widget _buildContent_0_800({
    required _AccountPageWlocState state,
    required BoxConstraints constraints,
    required BuildContext context,
  }) {
    return Stack(
      children: [
        Column(
          children: [
            _buildHeader(
              state: state,
              constraints: constraints,
              showBurgerIcon: true,
              context: context,
            ),
            Expanded(
              child: Scrollbar(
                child: CustomScrollView(
                  slivers: [
                    SliverFillRemaining(
                      hasScrollBody: false,
                      child: Column(
                        children: [
                          Expanded(
                            child: _buildFragment(
                              state: state,
                              width: constraints.maxWidth,
                              context: context,
                            ),
                          ),
                          const AppFooter(),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
        _buildDrawer(
          state: state,
          context: context,
        ),
      ],
    );
  }

  // ignore: non_constant_identifier_names
  Widget _buildContent_800_infinity({
    required _AccountPageWlocState state,
    required BoxConstraints constraints,
    required BuildContext context,
  }) {
    return Column(
      children: [
        _buildHeader(
          state: state,
          constraints: constraints,
          showBurgerIcon: false,
          context: context,
        ),
        Expanded(
          child: Scrollbar(
            child: LayoutBuilder(
              builder: (context, constraints) {
                return CustomScrollView(
                  primary: true,
                  slivers: [
                    SliverFillRemaining(
                      hasScrollBody: false,
                      child: Column(
                        children: [
                          Expanded(
                            child: IntrinsicHeight(
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                  _buildSideNavigationBar(
                                    state: state,
                                    context: context,
                                  ),
                                  Expanded(
                                    child: _buildFragment(
                                      state: state,
                                      width: constraints.maxWidth - _kSideNavigationWidth,
                                      context: context,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          const AppFooter(),
                        ],
                      ),
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildHeader({
    required _AccountPageWlocState state,
    required BoxConstraints constraints,
    required bool showBurgerIcon,
    required BuildContext context,
  }) {
    final maxWidth = constraints.maxWidth;

    if (maxWidth < kRegularHeaderMinWidth) {
      return _buildCompactHeader(
        state: state,
        showBurgerIcon: showBurgerIcon,
        context: context,
      );
    }

    return _buildRegularHeader(
      state: state,
      showBurgerIcon: showBurgerIcon,
      context: context,
    );
  }

  Widget _buildRegularHeader({
    required _AccountPageWlocState state,
    required bool showBurgerIcon,
    required BuildContext context,
  }) {
    final List<Widget> leadingWidgets;

    if (showBurgerIcon) {
      leadingWidgets = [
        const SizedBox(
          width: 8,
        ),
        _buildHeaderBurgerIcon(
          context: context,
        ),
      ];
    } else {
      leadingWidgets = [
        const SizedBox(
          width: 8,
        ),
        HeaderLogoButton(
          width: 200,
          onPressed: _wloc.onHeaderLogoButtonPressed,
        ),
      ];
    }

    return AppHeader(
      height: kRegularHeaderHeight,
      children: [
        ...leadingWidgets,
        const Spacer(),
        HeaderAngryWordsProRegularButton(
          onPressed: _wloc.onHeaderAngryWordsProButtonPressed,
        ),
        const SizedBox(
          width: 16,
        ),
        HeaderUserButton(
          iconSize: 35,
          onAccountTilePressed: _wloc.onHeaderUserActionDialogAccountTilePressed,
          onSigningOutTilePressed: _wloc.onHeaderUserActionDialogSigningOutTilePressed,
        ),
        const SizedBox(
          width: 16,
        ),
      ],
    );
  }

  Widget _buildCompactHeader({
    required _AccountPageWlocState state,
    required bool showBurgerIcon,
    required BuildContext context,
  }) {
    final List<Widget> leadingWidgets;

    if (showBurgerIcon) {
      leadingWidgets = [
        const SizedBox(
          width: 8,
        ),
        _buildHeaderBurgerIcon(
          context: context,
        ),
      ];
    } else {
      leadingWidgets = [
        const SizedBox(
          width: 8,
        ),
        HeaderLogoButton(
          width: 200,
          onPressed: _wloc.onHeaderLogoButtonPressed,
        ),
      ];
    }

    return AppHeader(
      height: kCompactHeaderHeight,
      children: [
        ...leadingWidgets,
        const Spacer(),
        HeaderAngryWordsProCompactButton(
          onPressed: _wloc.onHeaderAngryWordsProButtonPressed,
        ),
        const SizedBox(
          width: 8,
        ),
        HeaderUserButton(
          iconSize: 30,
          onAccountTilePressed: _wloc.onHeaderUserActionDialogAccountTilePressed,
          onSigningOutTilePressed: _wloc.onHeaderUserActionDialogSigningOutTilePressed,
        ),
        const SizedBox(
          width: 12,
        ),
      ],
    );
  }

  Widget _buildHeaderBurgerIcon({
    required BuildContext context,
  }) {
    return AppButton(
      pressedOpacity: 0.6,
      onPressed: _wloc.onHeaderBurgerIconPressed,
      child: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Icon(
          CupertinoIcons.bars,
          color: AccountPageTheme.of(context).headerBurgerIconColor,
          size: 30,
        ),
      ),
    );
  }

  Widget _buildDrawer({
    required _AccountPageWlocState state,
    required BuildContext context,
  }) {
    final drawer = _buildDrawerNavigationBar(
      state: state,
      context: context,
    );

    return DrawerController(
      key: _wloc.drawerKey,
      alignment: DrawerAlignment.start,
      child: drawer,
    );
  }

  Widget _buildSideNavigationBar({
    required _AccountPageWlocState state,
    required BuildContext context,
  }) {
    final activeFragmentModel = _wloc.widget.pageModel.fragmentModel;

    return Container(
      width: _kSideNavigationWidth,
      color: AccountPageTheme.of(context).navigationBarBackgroundColor,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(
            height: 16,
          ),
          _buildNavigationBarTile(
            title: AccountPageTranslation.of(context).accountOverviewNavigationBarTileTitle,
            active: activeFragmentModel is AccountOverviewAccountPageFragmentModel,
            iconData: CupertinoIcons.house,
            onPressed: _wloc.onAccountOverviewNavigationBarTilePressed,
            context: context,
          ),
          _buildNavigationBarTile(
            title: AccountPageTranslation.of(context).purchaseManagementNavigationBarTileTitle,
            active: activeFragmentModel is PurchaseManagementAccountPageFragmentModel,
            iconData: CupertinoIcons.star,
            onPressed: _wloc.onPurchaseManagementNavigationBarTilePressed,
            context: context,
          ),
          _buildNavigationBarTile(
            title: AccountPageTranslation.of(context).transactionHistoryNavigationBarTileTitle,
            active: activeFragmentModel is TransactionHistoryAccountPageFragmentModel,
            iconData: CupertinoIcons.clock,
            onPressed: _wloc.onTransactionHistoryNavigationBarTilePressed,
            context: context,
          ),
          _buildNavigationBarTile(
            title: AccountPageTranslation.of(context).paymentMethodNavigationBarTileTitle,
            active: activeFragmentModel is PaymentMethodAccountPageFragmentModel,
            iconData: CupertinoIcons.creditcard,
            onPressed: _wloc.onPaymentMethodNavigationBarTilePressed,
            context: context,
          ),
        ],
      ),
    );
  }

  Widget _buildDrawerNavigationBar({
    required _AccountPageWlocState state,
    required BuildContext context,
  }) {
    final activeFragmentModel = _wloc.widget.pageModel.fragmentModel;

    return Container(
      width: 300,
      height: double.infinity,
      color: AccountPageTheme.of(context).navigationBarBackgroundColor,
      child: Stack(
        fit: StackFit.expand,
        children: [
          Align(
            alignment: Alignment.topLeft,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const SizedBox(
                  height: 16,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 8,
                  ),
                  child: HeaderLogoButton(
                    width: 200,
                    onPressed: _wloc.onHeaderLogoButtonPressed,
                  ),
                ),
                const SizedBox(
                  height: 16,
                ),
                _buildNavigationBarTile(
                  title: AccountPageTranslation.of(context).accountOverviewNavigationBarTileTitle,
                  active: activeFragmentModel is AccountOverviewAccountPageFragmentModel,
                  iconData: CupertinoIcons.house,
                  onPressed: _wloc.onAccountOverviewNavigationBarTilePressed,
                  context: context,
                ),
                _buildNavigationBarTile(
                  title:
                      AccountPageTranslation.of(context).purchaseManagementNavigationBarTileTitle,
                  active: activeFragmentModel is PurchaseManagementAccountPageFragmentModel,
                  iconData: CupertinoIcons.star,
                  onPressed: _wloc.onPurchaseManagementNavigationBarTilePressed,
                  context: context,
                ),
                _buildNavigationBarTile(
                  title:
                      AccountPageTranslation.of(context).transactionHistoryNavigationBarTileTitle,
                  active: activeFragmentModel is TransactionHistoryAccountPageFragmentModel,
                  iconData: CupertinoIcons.clock,
                  onPressed: _wloc.onTransactionHistoryNavigationBarTilePressed,
                  context: context,
                ),
                _buildNavigationBarTile(
                  title: AccountPageTranslation.of(context).paymentMethodNavigationBarTileTitle,
                  active: activeFragmentModel is PaymentMethodAccountPageFragmentModel,
                  iconData: CupertinoIcons.creditcard,
                  onPressed: _wloc.onPaymentMethodNavigationBarTilePressed,
                  context: context,
                ),
              ],
            ),
          ),
          Positioned(
            top: 4,
            right: 4,
            child: _buildDrawerCloseButton(
              context: context,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildNavigationBarTile({
    required String title,
    required bool active,
    required IconData iconData,
    required VoidCallback onPressed,
    required BuildContext context,
  }) {
    final TextStyle textStyle;
    final Color iconColor;
    final Color activityIndicatorColor;

    if (active) {
      textStyle = AccountPageTheme.of(context).textTheme.navigationBarActiveTileTitle;
      iconColor = AccountPageTheme.of(context).navigationBarActiveTileIconColor;
      activityIndicatorColor = CoreTheme.of(context).primaryColor;
    } else {
      textStyle = AccountPageTheme.of(context).textTheme.navigationBarInactiveTileTitle;
      iconColor = AccountPageTheme.of(context).navigationBarInactiveTileIconColor;
      activityIndicatorColor = Colors.transparent;
    }

    return AppButton(
      onPressed: onPressed,
      child: SizedBox(
        height: 50,
        width: double.infinity,
        child: Align(
          alignment: Alignment.centerLeft,
          child: Row(
            children: [
              Container(
                width: 4,
                color: activityIndicatorColor,
              ),
              const SizedBox(
                width: 16,
              ),
              Icon(
                iconData,
                color: iconColor,
                size: 20,
              ),
              const SizedBox(
                width: 16,
              ),
              Flexible(
                child: Text(
                  title,
                  style: textStyle,
                  textAlign: TextAlign.start,
                  textWidthBasis: TextWidthBasis.longestLine,
                ),
              ),
              const SizedBox(
                width: 16,
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildDrawerCloseButton({
    required BuildContext context,
  }) {
    return AppButton(
      onPressed: _wloc.onDrawerCloseButtonPressed,
      child: SizedBox(
        height: 50,
        width: 50,
        child: Center(
          child: Icon(
            CupertinoIcons.xmark,
            size: 23,
            color: AccountPageTheme.of(context).drawerCloseButtonIconColor,
          ),
        ),
      ),
    );
  }

  Widget _buildFragment({
    required _AccountPageWlocState state,
    required double width,
    required BuildContext context,
  }) {
    final fragmentModel = _wloc.widget.pageModel.fragmentModel;

    if (fragmentModel is AccountOverviewAccountPageFragmentModel) {
      return AccountOverviewFragment(
        fragmentModel: fragmentModel,
        width: width,
      );
    }

    if (fragmentModel is PurchaseManagementAccountPageFragmentModel) {
      return PurchaseManagementFragment(
        fragmentModel: fragmentModel,
        width: width,
      );
    }

    if (fragmentModel is TransactionHistoryAccountPageFragmentModel) {
      return TransactionHistoryFragment(
        fragmentModel: fragmentModel,
        width: width,
      );
    }

    if (fragmentModel is PaymentMethodAccountPageFragmentModel) {
      return PaymentMethodFragment(
        fragmentModel: fragmentModel,
        width: width,
      );
    }

    throw UnimplementedError();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: CoreTheme.of(context).backgroundColor,
      width: double.infinity,
      height: double.infinity,
      child: LayoutBuilder(
        builder: (context, constraints) {
          return WlocBuilder<_AccountPageWloc, _AccountPageWlocState>(
            wloc: _wloc,
            builder: (context, state) {
              return _buildContent(
                state: state,
                constraints: constraints,
                context: context,
              );
            },
          );
        },
      ),
    );
  }
}
